package com.lc.zpyh.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.lc.zpyh.R;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.response.SelectOneLVBean;
import com.lc.zpyh.other.IntentKey;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2018/11/26.
 */

public class NeijigouRecyAdapter extends RecyclerView.Adapter<NeijigouRecyAdapter.ViewHolder> implements View.OnClickListener {

    public Context context;

    private onChildClickListener listener;
    private RecyclerView recyclerView;
    private List<SelectOneLVBean.MerchantClassifiesBean> list;

    private String tag = "NeijigouRecyAdapter";


    public NeijigouRecyAdapter(Context context, List<SelectOneLVBean.MerchantClassifiesBean> list) {
        this.context = context;
        this.list = list;
    }


    public void setOnChildClickListener(onChildClickListener listener) {

        this.listener = listener;

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_re_menu, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        //viewHolder.ratingBar

        viewHolder.setData(context,list.get(i),i);
    }


    @Override
    public int getItemCount() {
        if (list != null && !list.isEmpty()) {
            return list.size();
        } else {
            return 0;
        }

    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    //============================


    public void swap(List<SelectOneLVBean.MerchantClassifiesBean> datas) {
        if (datas != null) {
            list.clear();
            list.addAll(datas);
            notifyDataSetChanged();
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }


    @Override
    public void onClick(View v) {
        if (recyclerView != null && listener != null && !list.isEmpty()) {
            int position = recyclerView.getChildAdapterPosition(v);
            Log.i(tag, position + "");
            if (position > -1) {
                listener.onChildClick(recyclerView, v, position, list.get(position));
            }
        }
    }


    public void remove(int position) {
        list.remove(position);

        notifyItemRemoved(position);
    }


    public interface onChildClickListener {
        void onChildClick(RecyclerView parent, View view, int position, SelectOneLVBean.MerchantClassifiesBean data);
    }






    static  class ViewHolder  extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_menu_home)
        ImageView ivMenuHome;
        @BindView(R.id.tv_menu_title_home)
        TextView tvMenuTitleHome;
        @BindView(R.id.ll_menu_home)
        LinearLayout llMenuHome;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(Context context,SelectOneLVBean.MerchantClassifiesBean data,int i) {
            tvMenuTitleHome.setText(data.getClassifyName());
//            ivMenuHome.setImageResource(R.mipmap.ic_launcher);



                   if (data.getClassifyName().equals("邀请好友")){
                       ivMenuHome.setImageResource(R.mipmap.icon_home_8);
                   }else if (data.getClassifyName().equals("优惠券")){
                       ivMenuHome.setImageResource(R.mipmap.icon_home_7);
                   } else {
    GlideApp.with(context).load(IntentKey.CAR_IMAGE+data.getImg()).apply(new RequestOptions().placeholder(R.mipmap.icon_home_3).error(R.mipmap.icon_home_3)).into(ivMenuHome);
            }

        }

        int[] arr=  new int[]{R.mipmap.icon_home_8,R.mipmap.icon_home_7,
                R.mipmap.icon_home_3, R.mipmap.icon_home_4,
                R.mipmap.icon_home_5,R.mipmap.icon_home_6,
                R.mipmap.icon_home_1,R.mipmap.icon_home_2,R.mipmap.icon_home_8

        };
    }
}

